import requests
import os

def crawl_pxc_website():
    # 目标URL - 萍乡学院官网
    url = 'http://www.pxc.jx.cn/'
    
    # 设置请求头，进行UA伪装
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }
    
    try:
        # 发送HTTP GET请求
        response = requests.get(url, headers=headers, timeout=10)
        
        # 检查请求是否成功
        response.raise_for_status()
        
        # 自动检测编码，如果检测不到则使用utf-8
        response.encoding = response.apparent_encoding or 'utf-8'
        
        # 确保目录存在
        os.makedirs('D:\\', exist_ok=True)
        
        # 保存到文件
        save_path = 'D:\\萍乡学院.html'
        with open(save_path, 'w', encoding='utf-8') as f:
            f.write(response.text)
            
        print(f"页面已成功保存到 {save_path}")
        
    except requests.exceptions.RequestException as e:
        print(f"爬取过程中出现错误: {e}")
    except IOError as e:
        print(f"文件保存过程中出现错误: {e}")

if __name__ == '__main__':
    crawl_pxc_website()